package com.boc.langxiang.manager;

import android.content.Context;
import android.text.TextUtils;

import com.boc.langxiang.entity.response.UserInfo;
import com.boc.langxiang.util.GsonUtil;
import com.boc.langxiang.util.L;
import com.boc.langxiang.util.SPUtil;
import com.google.gson.reflect.TypeToken;

/**
 * CreateTime: 2016-06-23下午4:10
 * Author: wjkjinke00@126.com
 * Description:
 */
public class UserInfoManager {
    private Context mContext;
    private String mToken;
    public static final String SP_TOKEN = "token";
    public static final String SP_USERINFO = "userInfo";

    private static UserInfoManager sUserInfoManager = new UserInfoManager();
    private UserInfo mUserInfo;

    private UserInfoManager() {
    }

    public static UserInfoManager getInstance() {
        return sUserInfoManager;
    }

    public void initOnApplicationCreate(Context context) {
        mContext = context;
        mToken = (String) SPUtil.get(mContext, SP_TOKEN, "");
        String userInfoJson = (String) SPUtil.get(mContext, SP_USERINFO, "");
        L.i(userInfoJson);
        if (!TextUtils.isEmpty(userInfoJson)) {
            mUserInfo = GsonUtil.parseJson(userInfoJson, new TypeToken<UserInfo>() {
            });
        }
    }

    public UserInfo getUserInfo() {
        return mUserInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        mUserInfo = userInfo;
        SPUtil.put(mContext, SP_USERINFO, GsonUtil.toJsonStr(userInfo));
        setToken(userInfo.getToken());
    }

    public String getToken() {
        return mToken;
    }

    public void setToken(String token) {
        mToken = token;
        SPUtil.put(mContext, SP_TOKEN, token);
    }

    public void clearData() {
        mToken = "";
        mUserInfo = null;
        SPUtil.remove(mContext, SP_TOKEN);
        SPUtil.remove(mContext, SP_USERINFO);
    }

}
